﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace TraCuuTinRaoVat.UserControl
{
    public partial class QuanLyDanhMuc : System.Web.UI.UserControl
    {
         static List<String> dsDanhMucChinh = new List<string>();
         static List<String> dsDanhMucCon = new List<string>();
         //static int index = 1;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LinQ_RaoVatDataContext danhsach = new LinQ_RaoVatDataContext();
                var query = from Tin_Rao_Vat in danhsach.DANH_MUC_CHINHs
                            select Tin_Rao_Vat.TenDanhMucChinh;

                foreach (String nd in query)
                {
                    dsDanhMucChinh.Add(nd.Trim());
                }


                LinQ_RaoVatDataContext danhsach2 = new LinQ_RaoVatDataContext();
                var query2 = from Tin_Rao_Vat in danhsach2.DANH_MUC_CONs
                             select Tin_Rao_Vat.TenDanhMucCon;

                foreach (String nd in query2)
                {
                    dsDanhMucCon.Add(nd.TrimEnd().TrimStart());
                }
              //  txtTenDmChinh.Text = index.ToString();
            }
            //else
            //{
            //    txtTenDmChinh.Text = index.ToString();
            //}
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
             try
            {
                int temp = 1;
                for (int i = 0; i < dsDanhMucChinh.Count; i++)
                {
                    if (dsDanhMucChinh[i] == txtTenDanhMuc.Text.Trim())
                    {
                        temp = 0;
                    }
                  
                }
                if ((temp == 1)&& (txtTenDanhMuc.Text.Trim()!=""))
                {
                    LinQ_RaoVatDataContext danhsach = new LinQ_RaoVatDataContext();
                    DANH_MUC_CHINH dmChinh = new DANH_MUC_CHINH();
                    dmChinh.TenDanhMucChinh = txtTenDanhMuc.Text;
                    dmChinh.Deleted = false;
                    danhsach.DANH_MUC_CHINHs.InsertOnSubmit(dmChinh);
                    danhsach.SubmitChanges();
                    lblResultChinh.Text = "Thêm danh mục chính thành công";
                }
                else
                {
                    lblResultChinh.Text = "Trùng tên danh mục chính";
                }
            }
            catch (Exception ex)
            {
                lblResultChinh.Text = "Kiểm tra lại dữ liệu nhập";
            }
        }
   

        protected void btnDmCon_Click(object sender, EventArgs e)
        {
            try
            {
                int temp = 1;
                for (int i = 0; i < dsDanhMucCon.Count; i++)
                {
                    if (dsDanhMucCon[i] == txtTenDanhMucCon.Text.Trim())
                    {
                        temp = 0;
                    }

                }
                if ((temp == 1)&& (txtTenDanhMucCon.Text.Trim()!=""))
                {
                    LinQ_RaoVatDataContext danhsach = new LinQ_RaoVatDataContext();
                    DANH_MUC_CON dmCon = new DANH_MUC_CON();
                    var query = from Tin_Rao_Vat in danhsach.DANH_MUC_CHINHs
                                where Tin_Rao_Vat.TenDanhMucChinh.Trim() == DropDownList1.SelectedValue.ToString().Trim()
                                select Tin_Rao_Vat.MaDanhMucChinh;
                    int s = 0;
                    foreach (int nd in query)
                    {
                        s = nd;
                    }
                    dmCon.MaDanhMucChinh = s;
                    dmCon.TenDanhMucCon = txtTenDanhMucCon.Text;
                    dmCon.Deleted = false;
                    //dmCon.TrangThai = int.Parse(dropTrangThai2.SelectedValue.ToString());
                    danhsach.DANH_MUC_CONs.InsertOnSubmit(dmCon);
                    danhsach.SubmitChanges();
                    lblResultCon.Text = "Thêm danh mục con thành công";
                }


                else
                {
                    lblResultCon.Text = "Trùng tên danh mục con";
                }
            }

            catch (Exception ex)
            {
                lblResultCon.Text = "Kiểm tra lại dữ liệu nhập";
            }
        }

        protected void btnHuyCon_Click(object sender, EventArgs e)
        {
            txtTenDanhMucCon.Text = "";
        }

        protected void Button2_Click1(object sender, EventArgs e)
        {
            txtTenDanhMuc.Text = "";
        }

        protected void GridView3_SelectedIndexChanged(object sender, EventArgs e)
        {
           // index = GridView3.SelectedIndex;
          //  txtTenDmChinh.Text = txtTenDmChinh.ToString();
        }

        protected void GridView3_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
          //  index = GridView3.SelectedIndex;
        }

   
    }
}